import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login'
import home from '../components/Home'
import welcome from '../components/Welcome'
import users from '../components/user/Users'
import rights from '../components/power/rights'
import roles from '../components/power/roles'
import Cate from '../components/goods/Cate'
import Params from '../components/goods/Params'
import List from '../components/goods/List'
import AddGood from '../components/goods/Addgood'
import order from '../components/order/Order'
import Report from '../components/report/Report'


Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      {path: '/welcome', component: welcome},
      {path: '/users', component: users},
      {path: '/rights', component: rights},
      {path: '/roles', component: roles},
      {path: '/categories', component: Cate},
      {path: '/params', component: Params},
      {path: '/goods', component: List},
      {path: '/goods/add', component: AddGood},
      {path: '/orders', component: order},
      {path: '/reports', component: Report},
    ]
  }
]

const router = new VueRouter({
  routes
})

//路由权限控制
router.beforeEach((to, from, next) => {
  if (to.path == '/login') return next()

  const tokenStr = sessionStorage.getItem('token')
  if (!tokenStr) {
    next('/login')
  }
  next()
})
export default router
